public int countKDifference(int[] nums, int k) {
    int ans = 0;
    for (int i = 0; i < nums.length; i++) {
        for (int j = i+1; j < nums.length; j++) {
            if (nums[i] - nums[j] == k || nums[j] - nums[i] == k) {
                ans++;
            }
        }
    }
    return ans;
}
public int countKDifference1(int[] nums, int k) {
    int ans = 0;
    Map<Integer, Integer> map = new HashMap<>();
    for (int x : nums
    ) {
        ans += map.getOrDefault(x - k, 0) + map.getOrDefault(x + k, 0);
        map.put(x, map.getOrDefault(x, 0) + 1);
    }
    return ans;
}